﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Roles;
using MediatR;
using Microsoft.AspNetCore.Http;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Roles
{
    public class RoleCreateCommandHandler : IRequestHandler<RoleCreateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> roleRepos;
        private readonly IMapper mapper;

        public RoleCreateCommandHandler(IBaseRepository<Role> roleRepos,IMapper mapper)
        {
            this.roleRepos = roleRepos;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(RoleCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<Role>(request);  
            var res = roleRepos.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
